package com.zyg.controller;

import com.zyg.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/datas")
public class DataSController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/tolist")
    public String tolist(Model model) {
        return "data_screen/datasc";
    }

    @RequestMapping("/getAllData")
    @ResponseBody
    public Map<String, Object> getAllData() {
        // 获取年级分布数据
        List<Object[]> rawGradeDistribution = studentService.getGradeDistribution();
        List<Map<String, Object>> gradeDistribution = rawGradeDistribution.stream()
                .map(row -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("grade", row[0]);
                    map.put("count", row[1]);
                    return map;
                })
                .collect(Collectors.toList());

        // 获取专业分布数据
        List<Object[]> rawMajorDistribution = studentService.getMajorDistribution();
        List<Map<String, Object>> majorDistribution = rawMajorDistribution.stream()
                .map(row -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("major", row[0]);
                    map.put("count", row[1]);
                    return map;
                })
                .collect(Collectors.toList());

        // 获取学生总人数
        long totalCount = studentService.count();

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("gradeDistribution", gradeDistribution);
        result.put("majorDistribution", majorDistribution);
        result.put("totalCount", totalCount);

        return result;
    }

    @RequestMapping("/getHotelDistribution")
    @ResponseBody
    public Map<String, Object> getHotelDistribution() {
        // 获取宿舍分布数据
        List<Object[]> rawHotelDistribution = studentService.getHotelDistribution();
        List<Map<String, Object>> hotelDistribution = rawHotelDistribution.stream()
                .map(row -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("hotelId", row[0]);
                    map.put("count", row[1]);
                    return map;
                })
                .collect(Collectors.toList());

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("hotelDistribution", hotelDistribution);

        return result;
    }

    @RequestMapping("/getAgeDistribution")
    @ResponseBody
    public Map<String, Object> getAgeDistribution() {
        // 获取年龄分布数据
        List<Object[]> rawAgeDistribution = studentService.getAgeDistribution();
        List<Map<String, Object>> ageDistribution = rawAgeDistribution.stream()
                .map(row -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("age", row[0]);
                    map.put("count", row[1]);
                    return map;
                })
                .collect(Collectors.toList());

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("ageDistribution", ageDistribution);

        return result;
    }

}
